//
//  WalletViewController.m
//  WBBaseProject
//
//  Created by 吴斌 on 2018/3/12.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "WalletViewController.h"
#import "MyWalletView.h"
#import "RechargeViewController.h"
#import "SetTradPwdViewController.h"
#import "WithdrawalViewController.h"
#import "WalletDetailViewController.h"
#define kRechargeTag 1000
#define KDepositTag  1001

@interface WalletViewController ()
{
    NSString *allmoney;
}
@property (nonatomic, strong) MyWalletView *walletView;
@end

@implementation WalletViewController

#pragma mark - 生命周期 Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的钱包";
    self.view.backgroundColor = WB_RGBColor(240, 240, 240);
    
    [self addRightButton];
    
    [self setupUI];
    [self getData];
}
- (void)getData
{
    [XYHttpRequest dataRequestWithGetCustBalanaSuccess:^(id responseObj) {
       
        if (Success) {
            allmoney = responseObj[@"data"];
            self.walletView.moneyLb.text = [NSString stringWithFormat:@"￥%@",responseObj[@"data"]];
        }
    } failure:^(NSError *error) {
        
    }];
}
- (void)setupUI
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavigationBarHeight)];
    scrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight - kNavigationBarHeight + 1);
    scrollView.bounces = YES;
    scrollView.backgroundColor = WB_RGBColor(240, 240, 240);
    [self.view addSubview:scrollView];
    
    [scrollView addSubview:self.walletView];
}

- (void)addRightButton
{
    UIButton *btn = [UIButton ba_buttonWithFrame:CGRectMake(0, 0, 80, 64) title:@"钱包明细" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:13]];
    [btn ba_button_setButtonLayoutType:BAKit_ButtonLayoutTypeRightImageLeft padding:2.0f];
    btn.titleLabel.textAlignment = NSTextAlignmentRight;
    btn.frame = CGRectMake(0, 0, 80, 64);
    //把视图的边角变为圆形, cornerRadius圆角半径
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    //弹簧控件, 修复边距
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spaceItem.width = -5;
    self.navigationItem.rightBarButtonItems = @[spaceItem,backItem];
    [btn addTarget:self action:@selector(clickDetail:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)clickDetail:(UIButton *)sender
{
    WalletDetailViewController *vc = [[WalletDetailViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载  Lazy Load
- (MyWalletView *)walletView
{
    WB_WeakSelf(self)
    if (_walletView == nil) {
        _walletView = [[NSBundle mainBundle] loadNibNamed:@"MyWalletView" owner:self options:nil].firstObject;
        _walletView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        _walletView.click = ^(UIButton *button) {
            if (button.tag == kRechargeTag) {
                RechargeViewController *vc = [[RechargeViewController alloc] init];
                [weakself.navigationController pushViewController:vc animated:YES];
            }else if (button.tag == KDepositTag) {
                [weakself goWithdraw];
            }
        };
    }
    return _walletView;
}

- (void)goWithdraw{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"您的账户存在异常，已冻结余额4000元剩余1000可提现，存在疑问联系客服详情询问" preferredStyle:UIAlertControllerStyleAlert];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"去提现" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        WithdrawalViewController *vc = [[WithdrawalViewController alloc] init];
        vc.allMoney = allmoney;
        [self.navigationController pushViewController:vc animated:YES];
    }]];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
}


@end
